<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\Config;
use app\index\model\User;
use app\index\model\Course as Modelcourse;
use app\index\model\tc;
use app\index\model\sc;
use app\index\model\ss;
use app\index\model\Classes;

class Course extends Controller
{
    public function index(){
        $userId = Session::get("Uid");
        $hint = '';
        $this->assign('hint',$hint);

        $Ustatus=Session::get('Ustatus');
        $this->assign('Ustatus',$Ustatus);

        $list = new User();
        $studata = $list->SC($userId);
        $this->assign('stucourse',$studata);
        $teadata = $list->TC($userId);
        // dump($teadata->toArray());
        $this->assign('teacourse',$teadata);
        
        return $this->fetch();
    }
    public function isPass(){
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){
            $courseId = $requset->post("courseId");
            $userId = Session::get("Uid");
            $Modelss = new ss();
            $classlist = new Classes();
            $class = $classlist->coscla($courseId);
            // dump($class->toArray());
            foreach($class->toArray() as $cla){
                // dump($cla);
                $classId = $cla['class_Id'];
                $ispass = $Modelss -> isPass($classId,$userId);
                // dump($ispass->toArray());
                // return $ispass->toArray();
                if(!empty($ispass->toArray())){
                    die(json_encode($ispass));
                }
            }
        }
    }

    public function addcourse(){
        $hint = Session::get('Hint');
        $this->assign('hint',$hint);
        return $this->fetch();
    }

    public function doaddcourse(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            //var_dump($has);
            $userId = Session::get("Uid");
            $courseId = $has["Id"];
            $courseName = $has["Name"];

            $addcourse = new Modelcourse();
            $add = $addcourse->addcourse($courseId,$courseName);
            $newcourse = $addcourse->newcourse();

            $C_Id = $newcourse->toArray()["0"]["Id"];
            $addtc = new tc();
            $ad = $addtc->addcourse($C_Id,$userId);
        }
        if($add === false || $ad === false){
            $hint = '课程添加失败';
            Session::set('Hint',$hint);
            $this->redirect('index/Course/addcourse');
        }else{
            $this->redirect('index/Course/index');
        }
    }

    public function courselist(){
        $list = new Modelcourse();
        $data = $list->courselist();
        // dump($data->toArray());
        $this->assign('courselist',$data);
        $hint='<tr><td class="text-c" colspan="4">暂无任何数据</td></tr>';
        $this->assign('hint',$hint);
        return $this->fetch();
    }

    public function joincourse(){
        if(Request::instance()->param()){
            $hint = Session::get('Hint');
            $this->assign('hint',$hint);

            $has = Request::instance()->param();
            // dump($has);
            $courseId = $has["courseId"];
            Session::set('courseId',$courseId);

            $classlist = new Classes();
            $class = $classlist->coscla($courseId);
            // dump($class->toArray());
            $this->assign('classlist',$class);
            $empty='<option value="">暂无班级</option>';
            $this->assign('empty',$empty);
        }
        return $this->fetch();
    }

    public function dojoincourse(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            dump($has);
            if(empty($has['class'])){
                $hint='请选择班级,若无班级请联系相关老师';
                Session::set('Hint',$hint);
                $this->redirect('index/Course/joincourse');
            }else{
                $courseId = Session::get('courseId');
                $classId = $has['class'];
                $userId = Session::get('Uid');

                $sc = new sc();
                $addsc = $sc->joincourse($courseId,$userId);

                $ss = new ss();
                $addss = $ss->addstudent($classId,$userId,0);
                if($addsc === false || $addss === false){
                    $hint = '加入课程失败';
                    Session::set('Hint',$hint);
                    $this->redirect('index/Course/joincourse');
                }else{
                    Session::delete('Hint');
                    $this->redirect('index/course/index');
                }
            }
        }
    }

    public function settop(){
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){
            $userId = Session::get("Uid");
            $C_Id = $requset->post("courseId");
            $userstatus = $requset->post("userstatus");
            $istop = $requset->post("top");
            if($userstatus==1){
                $tcModel = new tc();
                $clear = $tcModel->cleartop($userId);
                if($istop == 0){
                    $settop = $tcModel->settop($userId,$C_Id);
                    if($clear===false || $settop === false){
                        $retData = ["flag"=>0];
                    }else{
                        $retData = ["flag"=>1];
                    }
                }else{
                    if($clear===false){
                        $retData = ["flag"=>0];
                    }else{
                        $retData = ["flag"=>1];
                    }
                }
            }else{
                $scModel = new sc();
                $clear = $scModel->cleartop($userId);
                if($istop == 0){
                    $settop = $scModel->settop($userId,$C_Id);
                    if($clear===false || $settop === false){
                        $retData = ["flag"=>0];
                    }else{
                        $retData = ["flag"=>1];
                    }
                }else{
                    if($clear===false){
                        $retData = ["flag"=>0];
                    }else{
                        $retData = ["flag"=>1];
                    }
                }
            }
            die(json_encode($retData));
        }
    }

    public function deletecourse(){
        Config::set("default_return_type","json");

        $requset = Request::instance();           
        if($requset->isAjax()){
            $C_Id = $requset->post("courseId");
            $userstatus = $requset->post("userstatus");
            if($userstatus==1){
                $tcModel = new tc();
                $delete = $tcModel->del($C_Id);
                if($delete === false){
                    $retData = ["flag"=>0];
                }else{
                    $retData = ["flag"=>1];
                }
            }else{
                $scModel = new sc();
                $delete = $scModel->del($C_Id);
                if($delete === false){
                    $retData = ["flag"=>0];
                }else{
                    $retData = ["flag"=>1];
                }
            }
            die(json_encode($retData));
        }
    }

    public function trash(){
        $userId = Session::get("Uid");
        $hint = '';
        $this->assign('hint',$hint);

        $Ustatus=Session::get('Ustatus');
        $this->assign('Ustatus',$Ustatus);

        $list = new User();
        $studata = $list->SC($userId);
        $this->assign('stucourse',$studata);
        $teadata = $list->TC($userId);
        // dump($teadata->toArray());
        $this->assign('teacourse',$teadata);
        
        return $this->fetch();
    }

    public function recourse(){
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){
            $C_Id = $requset->post("courseId");
            $userstatus = $requset->post("userstatus");
            if($userstatus==1){
                $tcModel = new tc();
                $re = $tcModel->re($C_Id);
                if($re === false){
                    $retData = ["flag"=>0];
                }else{
                    $retData = ["flag"=>1];
                }
            }else{
                $scModel = new sc();
                $re = $scModel->re($C_Id);
                if($re === false){
                    $retData = ["flag"=>0];
                }else{
                    $retData = ["flag"=>1];
                }
            }
            die(json_encode($retData));
        }
    }
}